﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Data;
using System.Web.Security;

using System.IO;
using System.Text.RegularExpressions;

using CooWork.Services;
using Coowork.Data.Mapping;

namespace CooWork.Models
{

    public class TaskEditableAttribute : AuthorizeAttribute
    {
        private CooWorkEntities context = new CooWorkEntities();
        protected override bool AuthorizeCore(HttpContextBase httpContext)
        {
            if (!httpContext.Request.IsAuthenticated)
                return false;

            var rd = httpContext.Request.RequestContext.RouteData;
            string currentAction = rd.GetRequiredString("action");
            string currentController = rd.GetRequiredString("controller");
            int taskId = 0;            
            int.TryParse(httpContext.Request.QueryString["taskId"], out taskId);   
            if (httpContext.User.IsInRole("Admin"))
            {
                return true;
            }

            string sql = string.Format(" select * from CooTask where ID={0}"
                + "   select AuthRole from UserAuth where AuthAction='{1}' and AuthController='{2}' and cid={3}", taskId, currentAction, currentController, 0);
            
            
            DataSet ds = DataProvider.GetDataSet(sql);
            try
            {
                if (ds.Tables.Count == 2)
                {
                    if (ds.Tables[1].Rows.Count == 0) return false;                 
                    string userGuid = Membership.GetUser().ProviderUserKey.ToString();
                    if ((ds.Tables[0].Rows[0]["EditableList"].ToString()).Contains(userGuid))
                    {
                        for (int i = 0; i < ds.Tables[1].Rows.Count; i++)
                        {
                            TaskEditableRole ter = (TaskEditableRole)Convert.ToInt32(ds.Tables[1].Rows[i][0]);
                            if (ter == TaskEditableRole.Editor) return true;
                        }                     
                    }
                    if ((ds.Tables[0].Rows[0]["ObserverList"].ToString()).Contains(userGuid))
                    {
                        for (int i = 0; i < ds.Tables[1].Rows.Count; i++)
                        {
                            TaskEditableRole ter = (TaskEditableRole)Convert.ToInt32(ds.Tables[1].Rows[i][0]);
                            if (ter == TaskEditableRole.Observer) return true;
                        }              
                    }
                    if ((ds.Tables[0].Rows[0]["Creator"].ToString()).Equals(userGuid) )
                    {
                        for (int i = 0; i < ds.Tables[1].Rows.Count; i++)
                        {
                            TaskEditableRole ter = (TaskEditableRole)Convert.ToInt32(ds.Tables[1].Rows[i][0]);
                            if (ter == TaskEditableRole.Creator) return true;
                        }       
                    }
                    if ((ds.Tables[0].Rows[0]["Owner"].ToString()).Equals(userGuid))
                    {
                        for (int i = 0; i < ds.Tables[1].Rows.Count; i++)
                        {
                            TaskEditableRole ter = (TaskEditableRole)Convert.ToInt32(ds.Tables[1].Rows[i][0]);
                            if (ter == TaskEditableRole.Owner) return true;
                        }      
                    }                    
                }
            }
            catch
            {
                return false;
            }





            return false;
        }
    }



}